<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Jhub1_Admin\Controller;

use Jhub1_Admin\Form\AddNewAgent;
use Jhub1_Admin\Model\FormFilter\AddNewAgentFilter;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Jhub1_Common\Entity\BO\UsersBO;
use Jhub1_Common\Entity\BO\AgentsBO;
use Jhub1_Admin\Form\Registration;
use Jhub1_Admin\Model\FormFilter\RegistrationFilter;
use Application\Model\KeyGenerator;
use Zend\Session\Container;
use Jhub1_Common\Entity\BO\EndpointValuesBO;

class HubController extends AbstractActionController
{
    public function indexAction()
    {
        /*        $translator = $this->getServiceLocator()->get('translator');
                $translator->setLocale("pl_PL");*/
        $firstTimeUser = false;
        $session_user = new Container('user');
        $userPO = $session_user->offsetGet('userobject');
        $userBO = new UsersBO($this->getServiceLocator());
        $userBO->hydrate($userPO);

        if ($userBO->isNewUser()) {
            $firstTimeUser = true;
        }

        $ag = new AgentsBO($this->getServiceLocator());
        $agents = $ag->getAgentsList($userBO->getUserPO());
        $epv = new EndpointValuesBO($this->getServiceLocator());
        $res = $epv->checkForNewEndpoints($agents);

        return new ViewModel(array(
            'firstTimeUser' => $firstTimeUser,
            'newEndpoints' => $res
        ));
    }

    public function helpAction()
    {
        return new ViewModel();
    }

    /**
     * This is modal content
     * @return ViewModel
     */
    public function addagentAction()
    {

        $form = new AddNewAgent();
        $request = $this->getRequest();
        if ($request->isPost()) {
            $aaFilter = new AddNewAgentFilter();
            $form->setInputFilter($aaFilter->getInputFilter());
            $form->setData($request->getPost());
            if ($form->isValid()) {
                $session_user = new Container('user');
                $userBO = new UsersBO($this->getServiceLocator());
                $userBO->hydrate($session_user->offsetGet('userobject'));
                $userPO = $userBO->getUserPO();
                $agentsBO = new AgentsBO($this->getServiceLocator());
                $formData = $form->getData();
                $agentsBO->addAgent($userPO, $formData['name'], $formData['description']);

            }
            /*                $formData = $form->getData();
                            $userBO = new UsersBO($this->getServiceLocator());
                            $userPO = $userBO->getUserByEmail($formData['email']);
                            if(null != $userPO) {
                                $email = new Mailer();
                            }
                        }*/
        }
        $view = new ViewModel();
        $view->setTerminal(true);
        return $view;
    }


}